package com.jnc.plug.mybatis;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.jnc.plug.mybatis.mp.generator.MyBatisPlusGenerator;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Goal which touches a timestamp file.
 * <p>
 * //@goal touch
 *
 * @phase process-sources
 */
@Mojo(name = "generator")
public class MyMojo extends AbstractMojo {

    @Parameter(property = "outputDirectory",
            defaultValue = "${project.build.directory}/generated-sources/mybatis-generator", required = true)
    private File outputDirectory;

    /**
     * Location of the configuration file.
     */
    @Parameter(property = "configurationFile",
            defaultValue = "${project.basedir}/src/main/resources/generatorConfig.properties", required = true)
    private File configurationFile;

    @Parameter(property = "clearHistoryFile" , defaultValue = "false", required = true)
    private String clearHistoryFile;

    public void execute() throws MojoExecutionException {

        getLog().info("输出路径: " + outputDirectory.getPath());
        getLog().info("配置文件路径: " + configurationFile.getPath());

        if (!configurationFile.isFile()) {
            getLog().error("请添加src/main/resources/generatorConfig.properties文件");
            return;
        }

        if (!outputDirectory.exists()) {
            outputDirectory.mkdirs();
        }

        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(configurationFile));
            String property = properties.getProperty("global.output.dir");
            if (StringUtils.isBlank(property)){
                properties.setProperty("global.output.dir", outputDirectory.getPath());
            }
            if("true".equalsIgnoreCase(clearHistoryFile)){
                deleteDirectory(new File(properties.getProperty("global.output.dir")));
            }
            MyBatisPlusGenerator generator = new MyBatisPlusGenerator(properties);
            generator.generate();
        } catch (IOException e) {
            getLog().error(e.getLocalizedMessage());
        }

    }
    public void deleteDirectory(File file) {

        if (file.isFile()) {
            file.delete();//清理文件
        } else {
            File list[] = file.listFiles();
            if (list != null) {
                for (File f : list) {
                    deleteDirectory(f);
                }
                file.delete();//清理目录
            }

        }
    }
}
